﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Common;
using Transcend.Utility;

namespace Transcend.Archive.CSScan.Store.Contract
{
    /// <summary>
    /// 图像存储的基类，抽像类。
    /// 已实现：OracleImageStore,FTPImageStore
    /// </summary>
    public abstract class ImageStore : IDisposable
    {
        /// <summary>
        /// 构造器
        /// </summary>
        /// <param name="storeId"></param>
        public ImageStore(decimal storeId)
        {
            _storeId = storeId;
            FileNameFormat = "\"{img.PageNo}\".PadLeft(4,'0') + \"-\" + \"{img.CredName}\" + \"-\" + \"{img.PageNum}\".PadLeft(3,'0')";
        }

        private decimal _storeId;

        /// <summary>
        /// 获取存储Id
        /// </summary>
        public decimal StoreId
        {
            get
            {
                return _storeId;
            }
            set
            {
                _storeId = value;
            }
        }

        protected List<ScanImage> _images = new List<ScanImage>();
        /// <summary>
        /// 添加图像
        /// </summary>
        /// <param name="imageNo"></param>
        /// <param name="img"></param>
        public void Add(ScanImage image)
        {
            if (_images.Count(it => it.ImageNo == image.ImageNo) > 0)
            {
                throw new Exception(string.Format("图像序号[{0}]已存在，不能重复添加。", image.ImageNo));
            }
            _images.Add(image);
        }
        /// <summary>
        /// 批量添加图像
        /// </summary>
        /// <param name="images"></param>
        public void Add(IEnumerable<ScanImage> images)
        {
            foreach (var item in images)
                Add(item);
        }

        /// <summary>
        /// 移除一个图像
        /// </summary>
        /// <param name="imageNo"></param>
        public void Remove(decimal imageNo)
        {
            var img = _images.FirstOrDefault(it => it.ImageNo == imageNo);
            if (img == null)
            {
                throw new Exception(string.Format("图像序号[{0}]不存在，不能移除。", imageNo));
            }
        }

        public void Remove(ScanImage image)
        {
            var index = _images.IndexOf(image);
            if (index < 0)
            {
                throw new Exception(string.Format("图像序号[{0}]不存在，不能移除。", image.ImageNo));
            }
            _images.Remove(image);
        }
        /// <summary>
        /// 移除所有图像
        /// </summary>
        public void RemoveAll()
        {
            _images.Clear();
        }

        /// <summary>
        /// 分析传入的描述符,描述符采用Json方式存储
        /// </summary>
        /// <param name="Descripts"></param>
        public abstract void Parse(string Descripts);

        /// <summary>
        /// 将所有数据存储到数据库
        /// </summary>
        public abstract void Save();

        /// <summary>
        /// 获取图像内容
        /// </summary>
        /// <param name="imageNo"></param>
        /// <returns></returns>
        public abstract byte[] GetImage(decimal instid, decimal imgNo);

        /// <summary>
        /// 删除图像
        /// </summary>
        public abstract int Delete(decimal instanceId, decimal imageNo);

        /// <summary>
        /// 释放资源
        /// </summary>
        public virtual void Dispose()
        {
            //throw new NotImplementedException();
        }

        /// <summary>
        /// 文件更名
        /// </summary>
        /// <param name="img"></param>
        public abstract void ChangeFileName(ScanImage img);

        /// <summary>
        /// 重新重命名
        /// </summary>
        /// <param name="imageNo"></param>
        /// <param name="newName"></param>
        public abstract void ChangeFileName(decimal imageNo, string newName);

        /// <summary>
        /// 文件名称格式
        /// </summary>
        public string FileNameFormat { get; set; }

        public virtual string GetFileName(ScanImageDetail img)
        {
            string fnformat = FileNameFormat.Format<ScanImageDetail>(img,"img",string.Empty);
            
            var lmbExpression = Transcend.Utility.LambdaParser.ExpressionParser.Parse<Func<ScanImageDetail, string>>("(Transcend.Archive.CSScan.Store.Contract.ScanImageDetail img)=>" + fnformat);
            return lmbExpression.Compile().Invoke(img);
        }
    }
}
